package com.dengyuanke.spring.cloud.weather.service;

import com.dengyuanke.spring.cloud.weather.vo.City;
import com.dengyuanke.spring.cloud.weather.vo.CityList;
import com.dengyuanke.spring.cloud.weather.vo.XmlBuilder;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Author: dyk
 * Create time: 2018/9/6  14:42
 */
@Service
public class CityDataServiceImpl implements CityDataService{
    @Override
    public List<City> listCity() throws Exception {

        Resource resource=new ClassPathResource("citylist.xml");
        BufferedReader br=new BufferedReader(new InputStreamReader(resource.getInputStream(),"utf-8"));
        StringBuffer buffer=new StringBuffer();
        String line="";
        while ((line=br.readLine())!=null){
            buffer.append(line);
        }
        br.close();
        CityList cityList= (CityList) XmlBuilder.xmlStrToObject(CityList.class,buffer.toString());
        return cityList.getCityList();
    }
}
